/**
 *
 * Copyright Pact Lab of H.I.T.
 *
 * Designed and Implemented by Grid Researching Group, 
 * Pact Lab, Harbin
 * 
 * This Project is part of the national 973 Project:
 * Internet Based Virtual Computing Environment
 *
 * http://pact518.hit.edu.cn
 * 
 * Author:       Meteor <meteorlxk@gmail.com> 
 * Copyright:    pact518 
 * Version:      1.0
 * Created:      2009-5-12 
 * LastModified: 2009-5-12
 */
package edu.hit.pact.pgse.crawler.download;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import edu.hit.pact.pgse.crawler.util.CustomHttpClient;

/**
 * @author meteorlxk
 *
 */
public abstract class AbstractFetcher {
	
	protected static final String DEFAULT_USER_AGENT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 6.1; .NET CLR 2.0.50727)";
	
	protected CustomHttpClient client;
	
	protected String defaultCharSet;
	
	protected int readTimeout;
	
	protected int maxContentLength;
	
	protected String userAgent;
	
	protected AbstractFetcher(CustomHttpClient client, String defalutCharSet,
			int readTimeout, int maxContentLength, String userAgent) {
		this.client = client;

		this.defaultCharSet = defalutCharSet;

		this.readTimeout = readTimeout;

		this.maxContentLength = maxContentLength;

		this.userAgent = userAgent;
		
	}
	
	protected void setRequestHeader(HttpMethod method, String refereUrl, String host, String lastModifiedTime, String eTags) {
		method.setRequestHeader("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"); 	
		
		if (refereUrl != null && !refereUrl.isEmpty()) {
			method.setRequestHeader("Referer", refereUrl);
		}//end if
		
		method.setRequestHeader("Accept-Language", "zh-cn"); 
		method.setRequestHeader("User-Agent", this.userAgent);
		
		if (host != null && !host.isEmpty()) {
			method.setRequestHeader("Host", host); 			
		}//end if
		
		method.setRequestHeader("Connection", "Keep-Alive"); 
		method.setRequestHeader("Cache-Control", "no-cache"); 
		
		if (lastModifiedTime != null && !lastModifiedTime.isEmpty()) {
			method.setRequestHeader("If-Modified-Since", lastModifiedTime);
		}//end if
		if (eTags != null && !eTags.isEmpty()) {
			method.setRequestHeader("If-None-Match", eTags);
		}//end if	
	}
	
	protected void setParameter(HttpMethod method) {
		method.setFollowRedirects(false);

		//set get timeout
		method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, readTimeout * 1000);
		//use the default retry handler : request at most three times
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
	}
}
